/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import java.util.Date;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Chitietphieunhaphang;
import pojo.Cuahang;
import pojo.Hoadonbanhang;
import pojo.Khachhang;
import pojo.Mathang;
import pojo.Nhavien;
import pojo.Phieunhaphang;
import util.HibernateUtil;

/**
 *
 * @author FT
 */
public class HoaDonBanHangDAO {
    public static List<Hoadonbanhang> layDanhSachHoaDon() {
        List<Hoadonbanhang> ds = null;
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            String hql = "select sv from Hoadonbanhang sv";
            Query query = session.createQuery(hql);
            ds = query.list();
        } catch (HibernateException ex) {
                //Log the exception
                System.err.println(ex);
        } finally {
            session.close();
        }
        return ds;
    }
    
    public static String MaTangTuDong() {
        String chuoi = "";
        String macuoicung = "";
        List<Hoadonbanhang> listpnh = HoaDonBanHangDAO.layDanhSachHoaDon();
        if (HoaDonBanHangDAO.layDanhSachHoaDon().size() == 0) {
                chuoi += "mhd001";
                return chuoi;
        }
        macuoicung += listpnh.get(HoaDonBanHangDAO.layDanhSachHoaDon().size() - 1).getIdhoadonbanhang();
        String index = macuoicung.substring(3);
        try {
            
                //System.out.println("Index la"+index+"");
                int chiso = Integer.valueOf(index);
                chiso++;

                if (chiso < 10) {
                    chuoi += "mhd00" + chiso;
                } else if (chiso >= 10 && chiso < 100) {
                    chuoi += "mhd0" + chiso;
                } else if (chiso >= 1000) {
                    chuoi += "mhd" + chiso;
                }
            
        } catch (Exception e) {
            chuoi += "NA";
        }
        return chuoi;
    }
    
    public static void Insert(String mahoadon, Date ngayban, String nhanvien, String cuahang, String khachhang, int thue, int tongtien){
           Session session = HibernateUtil.getSessionFactory().openSession();
         
           Hoadonbanhang hd = new Hoadonbanhang(mahoadon);
           hd.setNgayban(ngayban);
           Nhavien nv = NhanVienDAO.GetNhanVien(nhanvien);
           hd.setNhavien(nv);
           Cuahang ch = CuaHangDAO.GetCuaHang(cuahang);
           hd.setCuahang(ch);
           Khachhang kh = KhachHangDAO.GetKhachhang(khachhang);
           hd.setKhachhang(kh);
           hd.setThue(thue);
           hd.setTongtien(tongtien);
            
        try {
            session.beginTransaction();
           
            session.save(hd);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
    }
    
    public static void DeleteOne(String mahoadon){
        Session session = HibernateUtil.getSessionFactory().openSession();
        Hoadonbanhang hd = HoaDonBanHangDAO.GetHoaDon(mahoadon);
        try {
            session.beginTransaction();
           
            session.delete(hd);
            session.getTransaction().commit();
            
        } catch (HibernateException ex) {
            //Log the exception
            System.err.println(ex);
        } finally {
            session.close();
            
        }
    }
    
    public static Hoadonbanhang GetHoaDon(String mahoadon){
        Hoadonbanhang hd = new Hoadonbanhang();
        List<Hoadonbanhang> listhd = HoaDonBanHangDAO.layDanhSachHoaDon();
        for (int i=0;i<listhd.size();i++){
            if (mahoadon.equals(listhd.get(i).getIdhoadonbanhang())){
                hd = listhd.get(i);
            }
        }
        return hd;
    } 
}
